<template>
  <div class="cartcontrol">
    <transition name="move">
      <div class="cart-decrease icon-remove_circle_outline" v-if="food.count>0" @click.stop="decreaseCart">
        <span class="inner icon-remove_circle_outline"></span>
      </div>
    </transition>
    <div class="cart-count" v-if="food.count>0" >{{food.count}}</div>
    <div class="cart-add icon-add_circle"  @click.stop="addCart"></div>
  </div>
</template>

<script type="text/ecmascript-6">
  import Vue from 'vue';
  export default {
    props:{
      food:{
        type:Object,
      }
    },
    created(){
    },
    methods:{
      //加
      addCart(event){
        //兼容pc端
        if(!event._constructed){
          return
        }
        if(!this.food.count){
          Vue.set(this.food,"count",1);///注册一个新字段
        }else{
          this.food.count++
        }
      //  写入小球,给当前 + 添加add事件 当add触发，父组件也会触发，继而触发addFood
        //绑定一个add名，将event.target值绑定在add中传递到父组件中
        this.$emit('add', event.target);//提交'cart-add'事情给父组件，第二个是要传递的参数
      },
      //减
      decreaseCart(event){
        //兼容pc端
        if(!event._constructed){
          return
        }
        if(this.food.count>0){
          this.food.count--
        }
      }
    }
  }
</script>
<style lang="stylus" rel="stylesheet/stylus">
  .cartcontrol
    font-size:0
    // 动画enter最终状态
    .cart-decrease{
      display: inline-block
      padding: 6px
      transition:all 0.4s linear
      transform:translate3d(0,0,0)
      opacity:1
      .inner{
        display:inline-block
        text-align:center
        font-size:24px
        line-height:24px
        color: rgb(0, 160, 220)
        transition:all 0.4s linear
        transform:rotate(0deg)
      }
      // &.move-enter-active, &.move-leave-active{
      // 	transition: all 0.4s linear
      // }
      // 动画enter的初始状态 和 leave 的最终状态
      &.move-enter, &.move-leave-active{
        transition: all 0.4s linear
        opacity: 0
        transform: translate3d(24px, 0, 0)
        .inner{
          transform: rotate(180deg)
        }
      }
    }
    .cart-count{
      display: inline-block
      vertical-align:top
      text-align:center
      width:12px
      line-height: 24px
      padding-top: 6px
      font-size: 10px
      color: rgb(147, 153, 159)
    }
    .cart-add{
      display:inline-block
      vertical-align:top
      line-height:24px
      font-size:24px
      padding:6px
      color:rgb(0,160,220)
    }
</style>
